#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QString strLocalHostName = QHostInfo::localHostName();
    qDebug() << "Local Host Name:" << strLocalHostName;
    ui->label->setText(strLocalHostName);
    //int nID = QHostInfo::lookupHost("qt-project.org", this, SLOT(lookedUp(QHostInfo)));
    int nID = QHostInfo::lookupHost("192.168.1.102", this, SLOT(lookedUp(QHostInfo)));
    QHostInfo host = QHostInfo::fromName("192.168.1.102");
    if (host.error() != QHostInfo::NoError) {
        qDebug() << "Lookup failed:" << host.errorString();
        return;
    }

    qDebug() << "Found hostName:" << host.hostName();
}

void MainWindow::lookedUp(const QHostInfo &host)
{
    if (host.error() != QHostInfo::NoError) {
        qDebug() << "Lookup failed:" << host.errorString();
        return;
    }
    qDebug() << "Found hostName:" << host.hostName();

//    if (host.error() != QHostInfo::NoError) {
//        qDebug() << "Lookup failed:" << host.errorString();
//        return;
//    }

//    foreach (const QHostAddress &address, host.addresses()) {
//        // 输出IPV4、IPv6地址
//        if (address.protocol() == QAbstractSocket::IPv4Protocol)
//            qDebug() << "Found IPv4 address:" << address.toString();
//        else if (address.protocol() == QAbstractSocket::IPv6Protocol)
//            qDebug() << "Found IPv6 address:" << address.toString();
//        else
//            qDebug() << "Found other address:" << address.toString();
//    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
